﻿namespace Morus.Infrastructure
{
    using System.Collections;
    using System.Collections.Generic;

    public abstract class DirectBus<TMessage> : Disposable, IBus<TMessage>
    {
        private bool _committed = false;
        private bool _disposed = false;
        private readonly IMessageDispatcher _dispatcher;
        private readonly Queue<TMessage> _messageQueue = new Queue<TMessage>();
        private readonly object _queueLock = new object();
        private TMessage[] _backupMessages;

        public DirectBus(IMessageDispatcher dispatcher)
        {
            _dispatcher = dispatcher;
        }

        public void Push(TMessage message)
        {
            lock (_queueLock)
            {
                _messageQueue.Enqueue(message);
            }
        }

        public void Push(IEnumerable<TMessage> messages)
        {
            lock (_queueLock)
            {
                foreach (var o in messages)
                {
                    _messageQueue.Enqueue(o);
                }
            }
        }

        public void Clear()
        {
            lock (_queueLock)
            {
                _messageQueue.Clear();
            }
        }

        public void Commit()
        {
            lock (_queueLock)
            {
                _backupMessages = new TMessage[_messageQueue.Count];
                _messageQueue.CopyTo(_backupMessages, 0);
                while (_messageQueue.Count > 0)
                {
                    if (_dispatcher != null)
                    {
                        _dispatcher.DispatchMessage(_messageQueue.Dequeue());
                    }
                }
                _committed = true;
            }
        }

        private void Rollback()
        {
            lock (_queueLock)
            {
                if (_backupMessages != null && _backupMessages.Length > 0)
                {
                    _messageQueue.Clear();
                    foreach (var msg in _backupMessages)
                    {
                        _messageQueue.Enqueue(msg);
                    }
                }
                _committed = false;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    if (!_committed)
                    {
                        try
                        {
                            Commit();
                        }
                        catch
                        {
                            Rollback();
                            throw;
                        }
                    }
                }
            }

            base.Dispose(disposing);

            _disposed = true;
        }

        public IEnumerator<TMessage> GetEnumerator()
        {
            return _messageQueue.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
